package com.yiwei.zxp.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.QualificationIdentityActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.event.SettingShopEvent;
import com.yiwei.zxp.util.A2bigA;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.SureIndentityDilaog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-7-2.
 */

public class HasLicenseIdentityFragment extends BaseFragment {
    @BindView(R.id.img_license)
    ImageView imgLicense;
    @BindView(R.id.btn_license_add)
    LinearLayout btnLicenseAdd;
    @BindView(R.id.btn_retake_license)
    TextView btnRetakeLicense;
    @BindView(R.id.edit_license)
    EditText editLicense;
    @BindView(R.id.edit_register_code)
    EditText editRegisterCode;
    @BindView(R.id.img_idcard)
    ImageView imgIdcard;
    @BindView(R.id.btn_idcard_add)
    LinearLayout btnIdcardAdd;
    @BindView(R.id.btn_retake_idcard)
    TextView btnRetakeIdcard;
    @BindView(R.id.edit_realname)
    EditText editRealname;
    @BindView(R.id.btn_edit_realname)
    TextView btnEditRealname;
    @BindView(R.id.txt_idcard_no)
    TextView txtIdcardNo;
    @BindView(R.id.layout_idcard_identity)
    LinearLayout layoutIdcardIdentity;
    Unbinder unbinder;
    public String licence_name;
    public String idcard_front_img;
    public String idcard_no;
    public String licence_img;
    public String licence_no;
    public String realname;
    private SureIndentityDilaog dialog;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_has_bussiness_identity;
    }

    @Override
    protected void initView() {
        editRegisterCode.setTransformationMethod(new A2bigA());
        //如果想要再实现输入字符数量的限制，可以这么写,如果限制字符数为6，就在LengthFilter中传入参数6
        editLicense.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(20)});

        if (getArguments() != null) {
            licence_name = getArguments().getString(Const.SettingShopData.License.licence_name);
            idcard_front_img = getArguments().getString(Const.SettingShopData.License.idcard_front_img);
            idcard_no = getArguments().getString(Const.SettingShopData.License.idcard_no);
            licence_img = getArguments().getString(Const.SettingShopData.License.licence_img);
            licence_no = getArguments().getString(Const.SettingShopData.License.licence_no);
            realname = getArguments().getString(Const.SettingShopData.License.realname);
            if (!TextUtils.isEmpty(licence_name)) {
                editLicense.setText(licence_name);
            }
            if (!TextUtils.isEmpty(idcard_no)) {
                txtIdcardNo.setText(idcard_no);
            }
            if (!TextUtils.isEmpty(realname)) {
                editRealname.setText(realname);
            }
            if (!TextUtils.isEmpty(licence_no)) {
                editRegisterCode.setText(licence_no);
            }
            if (!TextUtils.isEmpty(idcard_front_img)) {
                Glide.with(getContext()).load(idcard_front_img).into(imgIdcard);
                dismissIdcard();
            }
            if (!TextUtils.isEmpty(licence_img)) {
                Glide.with(getContext()).load(licence_img).into(imgLicense);
                dismissLicense();
            }
        }


        dialog = new SureIndentityDilaog(getContext()).setOnChooseListener(new SureIndentityDilaog.OnChooseListener() {
            @Override
            public void onSuccess() {
                SettingShopEvent event = new SettingShopEvent();
                event.event_type = Const.SettingShopType.Qualification;
                event.type = "1";
                event.licence_name = licence_name;
                event.idcard_front_img = idcard_front_img;
                event.idcard_no = idcard_no;
                event.licence_img = licence_img;
                event.licence_no = licence_no;
                event.realname = realname;
                EventBus.getDefault().post(event);
                getActivity().finish();
            }
        });
    }

    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (!Util.isChinese(source.charAt(i))) {
                    return "";
                }
            }
            return null;
        }
    };


    public void showSubmitDialog() {
        if (TextUtils.isEmpty(UI.toString(editLicense))) {
            ToastUtil.makeText(getContext(), "执照名称不能为空");
            return;
        }
        if (TextUtils.isEmpty(UI.toString(txtIdcardNo))) {
            ToastUtil.makeText(getContext(), "身份证不能为空");
            return;
        }
        if (TextUtils.isEmpty(UI.toString(editRealname))) {
            ToastUtil.makeText(getContext(), "真实姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(UI.toString(editRegisterCode))) {
            ToastUtil.makeText(getContext(), "注册号不能为空");
            return;
        }
        if (TextUtils.isEmpty(idcard_front_img)) {
            ToastUtil.makeText(getContext(), "身份证照片不能为空");
            return;
        }
        if (TextUtils.isEmpty(licence_img)) {
            ToastUtil.makeText(getContext(), "营业执照照片不能为空");
            return;
        }
        licence_name = UI.toString(editLicense);
        idcard_no = UI.toString(txtIdcardNo);
        realname = UI.toString(editRealname);
        licence_no = editRegisterCode.getText().toString().replaceAll(" ", "").toUpperCase();
        dialog.setData(licence_name, idcard_no, licence_no, realname);
        dialog.show();
    }

    private void dismissLicense() {
        btnLicenseAdd.setVisibility(View.GONE);
        btnRetakeLicense.setVisibility(View.VISIBLE);
    }

    private void dismissIdcard() {
        btnIdcardAdd.setVisibility(View.GONE);
        btnRetakeIdcard.setVisibility(View.VISIBLE);
    }

    public void setData(String licence_name, String idcard_front_img, String idcard_no, String licence_img, String licence_no, String realname) {
        this.licence_name = licence_name;
        this.idcard_front_img = idcard_front_img;
        this.idcard_no = idcard_no;
        this.licence_img = licence_img;
        this.licence_no = licence_no;
        this.realname = realname;

    }

    public void setFocus(EditText edittext) {

        edittext.setFocusable(true);
        edittext.setFocusableInTouchMode(true);
        edittext.requestFocus();
        edittext.findFocus();
        InputMethodManager inputManager = (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(edittext, 0);
    }

    public void setIdcardImg(Bitmap url) {
        Glide.with(getContext()).load(url).into(imgIdcard);
        dismissIdcard();
    }

    public void setIdcardImg(String url, String idcard_no, String realname) {
        idcard_front_img = url;
        Glide.with(getContext()).load(idcard_front_img).into(imgIdcard);
        layoutIdcardIdentity.setVisibility(View.VISIBLE);
        txtIdcardNo.setText(idcard_no);
        editRealname.setText(realname);
        dismissIdcard();
    }

    public void setLicenseImg(String url) {
        licence_img = url;
        Glide.with(getContext()).load(licence_img).into(imgLicense);
        dismissLicense();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.btn_license_add, R.id.btn_retake_license, R.id.btn_idcard_add, R.id.btn_retake_idcard, R.id.btn_edit_realname})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_license_add:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(3);
                break;
            case R.id.btn_retake_license:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(3);
                break;
            case R.id.btn_idcard_add:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(1);
                break;
            case R.id.btn_retake_idcard:
                ((QualificationIdentityActivity) getActivity()).startTakePhoto(1);
                break;
            case R.id.btn_edit_realname:
                setFocus(editRealname);
                break;
        }
    }
}
